import unittest
from time import sleep
import requests

from api import log
from api.api_register_login import ApiRegisterLogin

from parameterized import parameterized

from utils import read_json, clear_data


class TestRegisterLogin(unittest.TestCase):
    # def setUpClass(cls) -> None:
    #     clear_data()

    # 初始化
    def setUp(self) -> None:
        # 获取session
        self.session = requests.session()
        log.info('正在初始化session对象:{}'.format(self.session))
        # 获取api实例
        self.reg = ApiRegisterLogin(self.session)

    # 结束
    def tearDown(self) -> None:
        # 关闭
        self.session.close()
        log.info('正在关闭session对象:{}'.format(self.session))

    # 获取图⽚验证码测试
    @parameterized.expand(read_json('register_login.json', 'img_code'))
    def test01_img_code(self, random, expect_data):
        try:
            # 1、调用图片验证码接口
            resp = self.reg.api_img_code(random)
            log.info('执行图片验证码响应状态码为:{}'.format(resp.status_code))
            # 2、查看相应状态码
            self.assertEqual(expect_data, resp.status_code)
            log.info('执行图片验证码断言通过')
        except Exception as e:
            # 日志
            log.error('断言失败，原因:{}'.format(e))
            # 抛出异常
            raise

    # 获取短信验证码测试
    @parameterized.expand(read_json('register_login.json', 'msg_code'))
    def test02_msg_code(self, phone, imgVerifyCode, expect_text):
        try:
            # 1、获取图片验证码，让session记录cookie
            self.reg.api_img_code(123)
            # 2、调用api接口
            resp = self.reg.api_msg_code(phone=phone, imgVerifyCode=imgVerifyCode)
            log.info('执行接口结果为:{}'.format(resp.text))
            # 3、查看响应结果
            self.assertEqual(200, resp.status_code)
            self.assertIn(expect_text, resp.text)
            log.info('执行短信验证码断言通过')
        except Exception as e:
            # 日志
            log.error('断言失败，原因:{}'.format(e))
            # 抛出异常
            raise

    # 注册测试
    @parameterized.expand(read_json('register_login.json', 'register'))
    def test03_register(self, phone, password, verifycode, phone_code,
                        expect_text):
        try:
            # 1、图片验证码接口
            self.reg.api_img_code(123)
            # 2、短信验证码接口
            self.reg.api_msg_code(phone=phone, imgVerifyCode=verifycode)
            # 3、调用api接口
            resp = self.reg.api_register(phone=phone, password=password, verifycode=verifycode, phone_code=phone_code)
            log.info('执行接口结果为:{}'.format(resp.text))
            # 4、查看接口
            self.assertIn(expect_text, resp.text)
            log.info('执行注册断言通过')
        except Exception as e:
            # 日志
            log.error('断言失败，原因:{}'.format(e))

            # 抛出异常
            raise

    # 登录测试
    @parameterized.expand(read_json('register_login.json', 'login'))
    def test04_login(self, keywords, password, expect_text):
        try:
            i = 0
            resp = None
            if "error" in password:
                while i < 3:
                    resp = self.reg.api_login(keywords=keywords, password=password)
                    i += 1
                    # 断言锁定
                print(resp.text)
                self.assertIn("锁定", resp.text)
                # 暂停60s
                sleep(60)
                # 测试登录成功
                resp = self.reg.api_login(keywords=keywords, password="test123")
                log.info('执行接口结果为:{}'.format(resp.text))
                # 断言登录成功
                self.assertIn(expect_text, resp.text)
                log.info('执行登录断言通过')
            else:
                # 1、调用登录接口
                resp = self.reg.api_login(keywords=keywords, password=password)
                log.info('执行接口结果为:{}'.format(resp.text))

                # 2、查看结果
                self.assertIn(expect_text, resp.text)
                log.info('执行登录断言通过')
        except Exception as e:
            # 日志
            log.error('断言失败，原因:{}'.format(e))
            # 抛出异常
            raise

    # 查看是否登录接口测试
    @parameterized.expand(read_json("register_login.json", "login_status"))
    def test05_login_status(self, status, expect_text):
        try:
            if status == '已登录':
                # 1、调用登录接口
                self.reg.api_login(keywords=13601031111, password="test123")
            # 2、调用查询状态接口
            resp = self.reg.api_login_status()
            log.info('执行接口结果为:{}'.format(resp.text))
            # 3、查看结果
            self.assertIn(expect_text, resp.text)
            log.info('执行登录断言通过')
        except Exception as e:
            # 日志
            log.error('断言失败，原因:{}'.format(e))
            # 抛出异常
            raise
